import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core';
import { WebGLService } from 'src/app/services/webgl.service';
import { Matrix4 } from 'src/app/util/matrix';

@Component({
    selector: 'lookat-trianle',
    templateUrl: './lookat-trianle.component.html',
    styleUrls: ['./lookat-trianle.component.scss']
})
export class LookatTrianleComponent implements OnInit, AfterViewInit, OnDestroy {
    private vertexShaderSource = `
        attribute vec4 a_Position;
        attribute vec4 a_Color;
        uniform mat4 u_ViewMatrix;
        varying vec4 v_Color;
        void main () {
            gl_Position = u_ViewMatrix * a_Position;
            v_Color = a_Color;
        } 
    `;
    private fragShaderSource = `
        #ifdef GL_ES
        precision mediump float;
        #endif

        varying vec4 v_Color;
        void main () {
            gl_FragColor = v_Color;
        }
    `;
    private gl: WebGLRenderingContext;
    constructor(private webglService: WebGLService) { }
    ngOnInit() { }
    ngAfterViewInit () {
        this.init();
    }
    ngOnDestroy () {}

    private init () {
        const canvas = document.querySelector('#myCanvas') as HTMLCanvasElement;
        this.gl = canvas.getContext('webgl');
        // 初始化 shader
        this.webglService.initShader(this.gl, this.vertexShaderSource, this.fragShaderSource);

        const n = this.initVertexBuffer(this.gl);

        this.gl.clearColor(0.0, 0.0, 0.0, 1.0);

        const program = this.webglService.getProgramByGLContext(this.gl);
        const u_ViewMatrix = this.gl.getUniformLocation(program, 'u_ViewMatrix');

        // 计算视图矩阵
        const viewMatrix = new Matrix4();
        // viewMatrix.setLookAt(0.0, 0., 1., 0, 0, 0, 0, 1, 0);
        viewMatrix.setLookAt(0.20, 0.25, 0.25, 0, 0, 0, 0, 1, 0);

        // 设置视图矩阵
        this.gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements);

        this.gl.clear(this.gl.COLOR_BUFFER_BIT);

        this.gl.drawArrays(this.gl.TRIANGLES, 0, n);
    }

    private initVertexBuffer (gl: WebGLRenderingContext) {
        const vertices = new Float32Array([
            // Vertex coordinates and color(RGBA)
            0.0,  0.5,  -0.4,  0.4,  1.0,  0.4, // The back green one
            -0.5, -0.5,  -0.4,  0.4,  1.0,  0.4,
            0.5, -0.5,  -0.4,  1.0,  0.4,  0.4, 
            
            0.5,  0.4,  -0.2,  1.0,  0.4,  0.4, // The middle yellow one
            -0.5,  0.4,  -0.2,  1.0,  1.0,  0.4,
            0.0, -0.6,  -0.2,  1.0,  1.0,  0.4, 
        
            0.0,  0.5,   0.0,  0.4,  0.4,  1.0,  // The front blue one 
            -0.5, -0.5,   0.0,  0.4,  0.4,  1.0,
            0.5, -0.5,   0.0,  1.0,  0.4,  0.4, 
        ]);
        const n = 9;    // 点的个数
        const byteLen = vertices.BYTES_PER_ELEMENT;

        // 创建缓冲区对象
        const vertexTexCoordBuffer = gl.createBuffer();
        
        // 将缓冲区对象绑定到目标
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);

        // 向缓冲区对象中写入数据
        gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

        const program = this.webglService.getProgramByGLContext(gl);
        const a_Position = gl.getAttribLocation(program, 'a_Position');
        const a_Color = gl.getAttribLocation(program, 'a_Color');

        // 将缓冲区对象分配给 a_Position 变量
        gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 6 * byteLen, 0);
        gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 6 * byteLen, 3 * byteLen);

        // 连接 a_Position 变量与分配给它的缓冲区对象
        gl.enableVertexAttribArray(a_Position);
        gl.enableVertexAttribArray(a_Color);

        // 解绑
        gl.bindBuffer(gl.ARRAY_BUFFER, null);

        return n;
    }
}